﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title></title>
	<style type="text/css">
		@import url('global.css');
		@import url('http://egorkhmelev.github.com/jslider/css/jquery.slider.min.css');
	</style>
	<script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
	<script language="javascript" type="text/javascript" src="http://people.iola.dk/olau/flot/jquery.flot.js"></script>
	<script language="javascript" type="text/javascript" src="http://egorkhmelev.github.com/jslider/js/jquery.slider.min.js"></script>

	<script language="javascript" type="text/javascript" src="voting-sites.js"></script>

	<script type="text/javascript">
		var nodesPlain = [];

		function makePlainSites(nodesTree, nodesPlain) {
			$.each(nodesTree, function (i, n) {
				if (n.subordinates == undefined)
					nodesPlain.push(n);
				else
					makePlainSites(n.subordinates, nodesPlain);
			});
		}

		function filterByPresence(sites, presenceMin, presenceMax, acceptor) {
			$.each(sites, function (i, site) {
				$.each(site.districts, function (i, district) {
					var presence = (district.validBulletins + district.invalidBulletins) / district.votersListed;

					if (presenceMin <= presence && presence <= presenceMax)
						acceptor(district);
				});
			});
		}

		function makeDistribution(sites, index, presenceMin, presenceMax) {
			var distribution = [];

			filterByPresence(sites, presenceMin, presenceMax, function (district) {
				var bin = Math.floor(100 * district.votes[index] / (district.validBulletins + district.invalidBulletins));

				if (distribution[bin] == undefined || distribution[bin] == null)
					distribution[bin] = [];
				distribution[bin].push(district);
			});
			return distribution;
		}

		function makeSeries(distribution) {
			var series = [];

			$.each(distribution, function (i, districts) {
				if (districts != undefined)
					series.push([i, districts.length]);
			});
			return series;
		}

		function updateSummary($summary, nodes, presenceMin, presenceMax) {
			var total = {
				districts: 0, enlisted: 0, voted: 0
			};
			var filtered = {
				districts: 0, enlisted: 0, voted: 0
			};

			filterByPresence(nodes, presenceMin, presenceMax, function (site) {
				++filtered.districts;
				filtered.enlisted += site.votersListed;
				filtered.voted += site.validBulletins + site.invalidBulletins;
			});

			filterByPresence(nodes, 0, 1, function (site) {
				++total.districts;
				total.enlisted += site.votersListed;
				total.voted += site.validBulletins + site.invalidBulletins;
			});

			$('.filtered-districts', $summary).text(filtered.districts + " (" + (100 * filtered.districts / total.districts).toFixed(1) + "%)");
			$('.filtered-enlisted', $summary).text(filtered.enlisted + " (" + (100 * filtered.enlisted / total.enlisted).toFixed(1) + "%)");
			$('.filtered-voted', $summary).text(filtered.voted + " (" + (100 * filtered.voted / total.voted).toFixed(1) + "%)");
			$('.total-districts', $summary).text(total.districts);
			$('.total-enlisted', $summary).text(total.enlisted);
			$('.total-voted', $summary).text(total.voted);
		}

		function rebuildChart(nodes, candidates, presenceMin, presenceMax, commonVScale) {
			var seriesArray = [];
			var options = { xaxis: { min: 0, max: 100} };

			if (!commonVScale) {
				options.yaxes = [];
			}

			for (var i = 0; i < candidates; ++i) {
				var d = makeDistribution(nodes, i, presenceMin, presenceMax);
				var s = makeSeries(d);
				var data = {
					data: s,
					label: votingResults.votingOptions[i]
				};

				if (!commonVScale) {
					data.yaxis = i + 1;
					options.yaxes.push({ show: false });
				}
				seriesArray.push(data);
			}

			var chart = $.plot($('#placeholder'), seriesArray, options);
		}

		function rebuildChartFull() {
			var r = $('#presense-range').val().split(';');
			rebuildChart(nodesPlain, 5, 0.01 * r[0], 0.01 * r[1], $('#use-common-vscale').attr('checked'));
			updateSummary($('.summary'), nodesPlain, 0.01 * r[0], 0.01 * r[1]);
		}

		$(function () {
			makePlainSites(votingResults.data, nodesPlain);

			$('#presense-range').slider({
				from: 0,
				to: 100,
				scale: [0, '|', 50, '|', 100],
				dimension: '&nbsp;%',
				skin: 'round_plastic',
				callback: rebuildChartFull
			});

			$('#use-common-vscale').change(rebuildChartFull);

			rebuildChartFull();
		});
	</script>
	<script type="text/javascript">
		var _gaq = _gaq || [];
		_gaq.push(['_setAccount', 'UA-30228618-1']);
		_gaq.push(['_trackPageview']);

		(function() {
			var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
			ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
			var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
		})();
	</script>
</head>
<body>
	<div class="layout">
		<h1>Выборы президента РФ 4 марта 2012 года. Данные с сайта izbirkom.ru.</h1>
		<h2>Зависимость кол-ва УИК от процента голосов за кандидата</h2>
		<div id="placeholder" style="width: 100%; height: 300px;">
		</div>
		<div class="summary">
			<p>Участков: <span class="filtered-districts"></span></p>
			<p>Зарегистрировано: <span class="filtered-enlisted"></span></p>
			<p>Проголосовало: <span class="filtered-voted"></span></p>
			<p>Всего участков: <span class="total-districts"></span></p>
			<p>Всего зарегистрировано: <span class="total-enlisted"></span></p>
			<p>Всего проголосовало: <span class="total-voted"></span></p>
		</div>
		<div>
			<p>
				<label for="use-common-vscale">
					Общая шкала по кол-ву участков
				</label>
				<input id="use-common-vscale" type="checkbox" value="" />
			</p>
			<p>
				<label for="presense-range">
					Фильтр по явке:</label><input id="presense-range" type="slider" value="0;100" /></p>
		</div>
	</div>
</body>
</html>
